//
//  ShmPartListModel.swift
//  ParsingCC
//
//  Created by ins-yangming on 2023/1/7.
//

import UIKit

// MARK: - 轨道信息
class ShmPartListModel: NSObject {

    var partgroups = [ShmPartgroupModel]();
    
    var scorePart = [ShmScorePartModel]();
    
    func getXmlElement() -> GDataXMLElement{
        let partListElement = GDataXMLNode.element(withName: "part-list");
        for i in 0..<self.scorePart.count {
            partListElement?.addChild(self.partgroups[i * 2].getXmlElement());
            partListElement?.addChild(self.scorePart[i].getXmlElement());
            partListElement?.addChild(self.partgroups[i * 2 + 1].getXmlElement());
        }
        return partListElement!;
    }
    
}

class ShmPartgroupModel : NSObject {
    
    var type = "";
    
    var number = "";
    
    var groupSymbol = "";
    
    func getXmlElement() -> GDataXMLElement{
        let partgroupElement = GDataXMLNode.element(withName: "part-group");
        if let typeAttribute = GDataXMLNode.attribute(withName: "type", stringValue: self.type) as? GDataXMLNode {
            partgroupElement?.addAttribute(typeAttribute);
        }
        
        if let numberAttribute = GDataXMLNode.attribute(withName: "number", stringValue: self.number) as? GDataXMLNode {
            partgroupElement?.addAttribute(numberAttribute);
        }
        
        if self.groupSymbol.count != 0 {
            let groupSymbolElement = GDataXMLNode.element(withName: "group-symbol", stringValue: self.groupSymbol);
            partgroupElement?.addChild(groupSymbolElement);
        }
        return partgroupElement!;
    }
}


class ShmScorePartModel : NSObject {
    
    var id = "P1";
    
    var partname = "Piano";
    
    var partNameDisplay = ["display-text" : "Piano"];
    
    var partAbbreviation = "Pno."
    
    var partAbbreviationDisplay = ["display-text" : "Pno."];
    
    var scoreInstrument = ShmScoreInstrumentModel();
    
    
    func getXmlElement() -> GDataXMLElement{
        let scorePartElement = GDataXMLNode.element(withName: "score-part");
        if let idAttribute = GDataXMLNode.attribute(withName: "id", stringValue: self.id) as? GDataXMLNode {
            scorePartElement?.addAttribute(idAttribute);
        }
        
        let partnameElement = GDataXMLNode.element(withName: "part-name", stringValue: self.partname);
        scorePartElement?.addChild(partnameElement);
        
        let partNameDisplayElement = GDataXMLNode.element(withName: "part-name-display");
       
        
        for (key , value) in self.partNameDisplay {
            let element = GDataXMLNode.element(withName: key, stringValue: value);
            partNameDisplayElement?.addChild(element);
        }
        scorePartElement?.addChild(partNameDisplayElement);
        
        let partAbbreviationElement = GDataXMLNode.element(withName: "part-abbreviation", stringValue: self.partAbbreviation);
        scorePartElement?.addChild(partAbbreviationElement);
        
        let partAbbreviationDisplayElement = GDataXMLNode.element(withName: "part-abbreviation-display");
       
        
        for (key , value) in self.partAbbreviationDisplay {
            let element = GDataXMLNode.element(withName: key, stringValue: value);
            partAbbreviationDisplayElement?.addChild(element);
        }
        scorePartElement?.addChild(partAbbreviationDisplayElement);
        
        scorePartElement?.addChild(self.scoreInstrument.getXmlElement());
        
        
        return scorePartElement!;
    }
}

class ShmScoreInstrumentModel : NSObject {
    
    var id = "P1-l1";
    
    var instrumentname = "Piano (2)";
    
    var instrumentSound = "keyboard.piano.grand";
    
    var solo = "";
    
    var virtualInstrument = [("virtual-library" , "General MIDI"), ("virtual-name" , "Acoustic Piano")];
    
    func getXmlElement() -> GDataXMLElement{
        let scoreInstrumentElement = GDataXMLNode.element(withName: "score-instrument");
        if let idAttribute = GDataXMLNode.attribute(withName: "id", stringValue: self.id) as? GDataXMLNode {
            scoreInstrumentElement?.addAttribute(idAttribute);
        }
        
        
        let instrumentnameElement = GDataXMLNode.element(withName: "instrument-name", stringValue: self.instrumentname);
        scoreInstrumentElement?.addChild(instrumentnameElement);
        
        let instrumentSoundElement = GDataXMLNode.element(withName: "instrument-sound", stringValue: self.instrumentSound);
        scoreInstrumentElement?.addChild(instrumentSoundElement);
        
        let soloElement = GDataXMLNode.element(withName: "solo", stringValue: self.solo);
        scoreInstrumentElement?.addChild(soloElement);
        
        let virtualInstrumentElement = GDataXMLNode.element(withName: "virtual-instrument");
        for (key , value) in self.virtualInstrument {
            let element = GDataXMLNode.element(withName: key, stringValue: value);
            virtualInstrumentElement?.addChild(element);
        }
        
        scoreInstrumentElement?.addChild(virtualInstrumentElement);
        

        
        return scoreInstrumentElement!;
    }
}

